export default {
	//角度degree --> 弧度radian
	deg2rad(degree) {
		var r = (degree / 180) * Math.PI;
		return r;
	},
	
	// 根据经纬度计算两点之间距离
	getDistanceFromLatLonInKm(lon1, lat1, lon2, lat2) {
		const R = 6371; // 地球半径（千米）
		const dLat = this.deg2rad(lat2 - lat1); // deg2rad函数用于将角度转换为弧度
		const dLon = this.deg2rad(lon2 - lon1);
		const a =
			Math.sin(dLat / 2) * Math.sin(dLat / 2) +
			Math.cos(this.deg2rad(lat1)) * Math.cos(this.deg2rad(lat2)) *
			Math.sin(dLon / 2) * Math.sin(dLon / 2);
		const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
		const distance = R * c; // 单位：千米
		return distance.toFixed(2);
	},
	getLocation() {
		const that = this;
		return new Promise((resolve, reject) => {
			uni.getLocation({
				type: 'gcj02', // 坐标系类型
				success: function(res) {
					var latitude = res.latitude; // 维度
					var longitude = res.longitude; // 经度
					const coordinate = {
						longitude,
						latitude
					}
					resolve(coordinate);
				},
				fail: function(res) {
					that.showRefuseLocationPermission()
				}
			})
		})
	},
	// 用户拒绝授权的展示
	showRefuseLocationPermission() {
	    const that = this;
	    wx.showModal({
	        title: "提示",
	        content: "需要获取用户位置权限",
	        confirmText: "前往设置",
	        showCancel: false,
	        success(res) {
	            if (res.confirm) {
	                uni.openSetting({
	                    success: (res) => {
	                        console.log("打开设置成功", res);
	                        if (res.authSetting['scope.userLocation']) {
	                            console.log('成功授权userLocation')
	                            that.getLocation()
	                        } else {
	                            console.log('用户未授权userLocation')
	                            // 递归调用本函数，(函数套函数)
	                            that.showRefuseLocationPermission()
	                        }
	                    },
	                    fail: (err) => {
	                        console.log("打开设置失败", err)
	                    }
	                })
	            }
	        }
	    })
	},

}